﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Smartec.Accounts.Camel.AMS.WebApp.Models;
using Smartec.Accounts.Camel.AMS.Model.Entities;
using Smartec.Accounts.Camel.AMS.Model.Facades;

namespace Smartec.Accounts.Camel.AMS.WebApp.Controllers
{
    public class AccountController : Controller
    {

        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        // **************************************
        // URL: /Account/LogOn
        // **************************************

        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                   
                        FormsService.SignIn(model.UserName, model.RememberMe);
                        if (Url.IsLocalUrl(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index", "Home");
                        }
                   
                   
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        // **************************************
        // URL: /Account/LogOff
        // **************************************

        public ActionResult LogOff()
        {
            FormsService.SignOut();

            return RedirectToAction("Index", "Home");
        }

        // **************************************
        // URL: /Account/Register
        // **************************************

        //public ActionResult Register()
        //{
        //    ViewBag.PasswordLength = MembershipService.MinPasswordLength;
        //    return View();
        //}

        //[HttpPost]
        //public ActionResult Register(RegisterModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        // Attempt to register the user
        //        MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.UserName);

        //        if (createStatus == MembershipCreateStatus.Success)
        //        {
        //            Roles.AddUserToRole(Membership.GetUser(model.UserName).UserName, "Senior");
        //            Guid id = (Guid)Membership.GetUser(model.UserName).ProviderUserKey;
                    

        //            SiteUser siteUser = new SiteUser()
        //            {
        //                ID = id,
        //                DisplayName_EN = model.DisplayName_EN,
        //                DisplayName_AR = model.DisplayName_AR,
        //                StartDate = Convert.ToDateTime(model.StartDate),
        //                EndDate = Convert.ToDateTime(model.EndDate),
        //                Custodian_ID = model.Custodian_ID,
        //                IsActive = model.IsActive
        //            };

        //            SiteUserFacade.Add(siteUser);
        //            SiteUserFacade.Save();

        //            FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
        //            return RedirectToAction("Index", "Home");
        //        }
        //        else
        //        {
        //            ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
        //        }
        //    }

        //    // If we got this far, something failed, redisplay form
        //    ViewBag.PasswordLength = MembershipService.MinPasswordLength;
        //    return View(model);
        //}

        // **************************************
        // URL: /Account/ChangePassword
        // **************************************

        [Authorize]
        public ActionResult ChangePassword()
        {
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
                {
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                }
            }

            // If we got this far, something failed, redisplay form
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View(model);
        }

        // **************************************
        // URL: /Account/ChangePasswordSuccess
        // **************************************

        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }

        // **************************************
        // URL: /Account/EditUser
        // **************************************

        //[Authorize]
        //public ActionResult EditUser()
        //{
        //    SiteUser siteUser = SiteUserFacade.GetSiteUser((Guid)Membership.GetUser(HttpContext.User.Identity.Name).ProviderUserKey);
        //    if (siteUser == null)
        //    {
        //        RedirectToAction("LogOn");
        //    }
        //    EditUserModel model = new EditUserModel()
        //    {
        //        UserName = HttpContext.User.Identity.Name,
        //        DisplayName_EN = siteUser.DisplayName_EN,
        //        DisplayName_AR = siteUser.DisplayName_AR,
        //        StartDate = siteUser.StartDate,
        //        EndDate = siteUser.EndDate,
        //        Custodian_ID = siteUser.Custodian_ID,
        //        IsActive = siteUser.IsActive
        //    };

        //    return View(model);
        //}

        //[Authorize]
        //[HttpPost]
        //public ActionResult EditUser(EditUserModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        SiteUser siteUser = SiteUserFacade.GetSiteUser((Guid)Membership.GetUser(HttpContext.User.Identity.Name).ProviderUserKey);
        //        if (siteUser == null)
        //        {
        //            RedirectToAction("LogOn");
        //        }
        //        Guid id = siteUser.ID;
                
        //       siteUser.DisplayName_EN = model.DisplayName_EN;
        //        siteUser.DisplayName_AR = model.DisplayName_AR;
        //        siteUser.StartDate = model.StartDate;
        //        siteUser.EndDate = model.EndDate;
        //        siteUser.Custodian_ID = model.Custodian_ID;
        //        siteUser.IsActive = model.IsActive;

        //        SiteUserFacade.Save();
        //        RedirectToAction("EditUserSuccess");
        //    }
        //    // If we got this far, something failed, redisplay form
        //    return View(model);
        //}

        // **************************************
        // URL: /Account/EditUserSuccess
        // **************************************

        //public ActionResult EditUserSuccess()
        //{
        //    return View();
        //}

    }
}
